package com.balloon.servlet;

import com.balloon.logic.UserBean;
import com.balloon.io.UserDAO;
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * The Servlet is mainly the logic of the application,
 * It processes the data entered by the user, and redirects him to the appropriate JSP 
 */
public class LoginServlet extends HttpServlet {

    /**
     * Process get request.
     * 
     * @param request
     * @param response
     * @throws ServletException
     * @throws java.io.IOException
     */
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, java.io.IOException {

        try {
            String email = request.getParameter("email");
            if (email.equals("admin@gmail.com")) {
                // Create an instant of the bean 'UserBean'
                UserBean user = new UserBean();
                user.setEmailAddress(request.getParameter("email"));
                // in the fields named "un" and "pw", And save them to the bean
                user.setPassword(request.getParameter("pw"));
                // Call the DAO to ask the DB about the validity of this user
                user = UserDAO.login(user);

                if (user.isValid()) {
                    // Create a new session instance that will be with us throughout all the jsp pages
                    HttpSession session = request.getSession(true);
                    // Save the bean in the session to the variable
                    // "currentSessionUser", so that userLogged.jsp can use it
                    session.setAttribute("currentuser", user);
                    response.sendRedirect("admin.jsp");
                } else {
                    // Call a Please-Sign-up-fist page
                    response.sendRedirect("invalidLogin.jsp");
                }

            } else {
                // Create an instant of the bean 'UserBean'
                UserBean user = new UserBean();
                user.setEmailAddress(request.getParameter("email"));
                // in the fields named "un" and "pw", And save them to the bean
                user.setPassword(request.getParameter("pw"));

                user = UserDAO.login(user);
                if (user.isValid()) {
                    // Create a new session instance that will be with us
                    // throughout all the jsp pages
                    HttpSession session = request.getSession(true);
                    // Save the bean in the session to the variable
                    // "currentSessionUser", so that userLogged.jsp can use it
                    session.setAttribute("currentuser", user);
                    // Call a Welcome page
                    response.sendRedirect("home.jsp");
                } else {
                    // Call a Please-Sign-up-fist page
                    response.sendRedirect("invalidLogin.jsp");
                }
            }
        } catch (Throwable theException) {
            System.out.println(theException);
        }

    }
}
